﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;

namespace DimensionAddIn.V2
{
    public class XSheetRender
    {
        public void Render(Excel.Worksheet worksheet, XTemplate xTemplate)
        {
            RenderSchema(worksheet, xTemplate);
        }

        protected void RenderSchema(Excel.Worksheet worksheet, XTemplate xTemplate)
        {
            /*
            _worksheet = Globals.ThisAddIn.Application.ActiveSheet;

            _yMergeDict = new Dictionary<int, MergeInfo>();
            _xMergeDict = new Dictionary<int, MergeInfo>();

            DTree yTree = schema.YTree;
            DTree xTree = schema.XTree;
            List<PageDim> pageDims = schema.PageDims;*/

            int beginRow = 5;
            int firstRow = 5;
            int beginCol = 2;
            int firstCol = 2;


            int ydCount = xTemplate.YDims.Count;
            int xdCount = xTemplate.XDims.Count;
            /*
            int yvdCount = GetVisibleYDimCount(schema);
            // 总的需要显示的x维度表数量
            int xvdCount = GetVisibleXDimCount(schema);

            firstCol += yvdCount;
            firstRow += xvdCount;*/

            List<XDRow> yRows = xTemplate.YRows;
            List<XDRow> xRows = xTemplate.XRows;

            // 当前可见的y维度行的行号索引列表
            List<int> visualYRows = new List<int>();
            // 当前可见的x维度行的行号索引列表
            List<int> visualXRows = new List<int>();

            #region 清除当前内容区域
            /*
            if (docMatrix.VisualRect.IsValid())
            {
                Debug.WriteLine("clear rect:{0},{1}-{2},{3}",
                    docMatrix.VisualRect.BeginRow,
                    docMatrix.VisualRect.BeginCol,
                    docMatrix.VisualRect.EndRow,
                    docMatrix.VisualRect.EndCol);
                Excel.Range ltCell = _worksheet.XGetCell(docMatrix.VisualRect.BeginRow, docMatrix.VisualRect.BeginCol);
                Excel.Range rbCell = _worksheet.XGetCell(docMatrix.VisualRect.EndRow, docMatrix.VisualRect.EndCol);
                _worksheet.Range[ltCell, rbCell].Clear();
            }*/
            #endregion

            #region 渲染表头
            /*
            // 表头显示的行维的名称
            int headerIndex = 0;
            for (int index = 0; index < yTree.Table.Tables.Count; index++)
            {
                if (IsYDimVisible(yTree.Table.Tables[index].Code, schema))
                {
                    Excel.Range rowTitleRange = _worksheet.XGetCell(beginRow, beginCol + headerIndex);
                    rowTitleRange.Value2 = yTree.Table.Tables[index].Name;
                    Excel.Range mergeRange = _worksheet.XGetRange(
                        beginRow,
                        beginCol + headerIndex,
                        beginRow + xvdCount - 1,
                        beginCol + headerIndex);
                    mergeRange.Merge(rowTitleRange.MergeCells);

                    mergeRange.Interior.Color = 0xffcf9c;
                    mergeRange.Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight = Excel.XlBorderWeight.xlThin;
                    mergeRange.Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = Excel.XlBorderWeight.xlThin;
                    mergeRange.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlThin;
                    mergeRange.Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThin;
                    mergeRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
                    headerIndex++;
                }
            }*/
            #endregion
            /*
            #region 渲染y维度
            */
            int aRowIndex = 0;
            for (int rowIndex = 0; rowIndex < yRows.Count; rowIndex++)
            {
                XDRow dtRow = yRows[rowIndex];

                bool isVisible = true;
                /*
                #region 判断此行是否被页面维度过滤
                foreach (var pageDim in pageDims)
                {
                    if (pageDim.SelectedNames.Count > 0 && pageDim.Direction == DimensionData.DimDirection.Y)
                    {
                        string cpdName = dtRow.Items[pageDim.Index].Name;
                        if (!pageDim.SelectedNames.Contains(cpdName))
                        {
                            isVisible = false;
                            break;
                        }
                    }
                }
                #endregion
                */

                if (isVisible)
                {
                    // 在显示的维度行记录中，添加当前维度行号
                    visualYRows.Add(rowIndex);

                    // 行维的维度名称实际的渲染列索引
                    int dActualIndex = 0;
                    for (int dIndex = 0; dIndex < ydCount; dIndex++)
                    {
                        string codePath = dtRow.GetCodePath(dIndex);
                        XDItem dItem = dtRow.Items[dIndex];

                        /*
                        // 判断维度表是否需要显示
                        if (!IsYDimVisible(dIndex, schema))
                        {
                            continue;
                        }*/

                        /*
                        MergeInfo mergeInfo = null;*/
                        bool isRepeat = false;
                        /*
                        if (_yMergeDict.ContainsKey(dIndex))
                        {
                            MergeInfo existMerge = _yMergeDict[dIndex];
                            if (existMerge.CodePath == codePath)
                            {
                                existMerge.RepeatCount++;
                                isRepeat = true;
                            }
                            else
                            {
                                mergeInfo = existMerge;

                                MergeInfo newMergeInfo = new MergeInfo();
                                newMergeInfo.BeginCell = _worksheet.XGetCell(firstRow + aRowIndex, beginCol + dActualIndex);
                                newMergeInfo.RepeatCount = 0;
                                newMergeInfo.CodePath = codePath;
                                _yMergeDict[dIndex] = newMergeInfo;
                            }
                        }
                        else
                        {
                            mergeInfo = new MergeInfo();
                            mergeInfo.BeginCell = _worksheet.XGetCell(firstRow + aRowIndex, beginCol + dActualIndex);
                            mergeInfo.RepeatCount = 0;
                            mergeInfo.CodePath = codePath;
                            _yMergeDict.Add(dIndex, mergeInfo);
                        }

                        if (mergeInfo != null && mergeInfo.RepeatCount > 0)
                        {
                            Excel.Range mergeRange = _worksheet.XGetRange(
                                mergeInfo.BeginCell.Row,
                                mergeInfo.BeginCell.Column,
                                mergeInfo.BeginCell.Row + mergeInfo.RepeatCount,
                                mergeInfo.BeginCell.Column);
                            _worksheet.XGetRange(
                                mergeInfo.BeginCell.Row,
                                mergeInfo.BeginCell.Column,
                                mergeInfo.BeginCell.Row + mergeInfo.RepeatCount,
                                mergeInfo.BeginCell.Column).Merge(mergeInfo.BeginCell.MergeCells);


                            mergeRange.Interior.Color = 0xffcf9c;
                            mergeRange.Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight = Excel.XlBorderWeight.xlThin;
                            mergeRange.Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = Excel.XlBorderWeight.xlThin;
                            mergeRange.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlThin;
                            mergeRange.Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThin;
                        }
                        */
                        if (!isRepeat)
                        {
                            // 重复出现的维度名称，不在创建到excel上
                            Excel.Range range = worksheet.XGetCell(firstRow + aRowIndex, beginCol + dActualIndex);
                            range.Value2 = dtRow.Items[dIndex].Name;
                            range.IndentLevel = dtRow.Items[dIndex].Level;
                            range.Interior.Color = 0xffcf9c;
                            range.Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight = Excel.XlBorderWeight.xlThin;
                            range.Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = Excel.XlBorderWeight.xlThin;
                            range.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlThin;
                            range.Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThin;
                        }
                        dActualIndex++;
                    }
                    aRowIndex++;
                }
            }
            /*
            #region 检查最后的单元格
            foreach (var ymergeKey in _yMergeDict.Keys)
            {
                MergeInfo mergeInfo = _yMergeDict[ymergeKey];
                Excel.Range mergeRange = _worksheet.XGetRange(mergeInfo.BeginCell.Row, mergeInfo.BeginCell.Column,
                    mergeInfo.BeginCell.Row + mergeInfo.RepeatCount,
                    mergeInfo.BeginCell.Column);
                _worksheet.XGetRange(mergeInfo.BeginCell.Row, mergeInfo.BeginCell.Column,
                    mergeInfo.BeginCell.Row + mergeInfo.RepeatCount,
                    mergeInfo.BeginCell.Column).Merge(mergeInfo.BeginCell.MergeCells);


                mergeRange.Interior.Color = 0xffcf9c;
                mergeRange.Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight = Excel.XlBorderWeight.xlThin;
                mergeRange.Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = Excel.XlBorderWeight.xlThin;
                mergeRange.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlThin;
                mergeRange.Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThin;
            }
            #endregion
            #endregion

            #region 渲染x维度*/
            int aColIndex = 0;
            for (int rowIndex = 0; rowIndex < xRows.Count; rowIndex++)
            {
                XDRow dtRow = xRows[rowIndex];

                bool isVisible = true;
                /*
                #region 判断是否被页面维过滤
                foreach (var pageDim in pageDims)
                {
                    if (pageDim.Direction == DimensionData.DimDirection.X && pageDim.SelectedNames.Count > 0)
                    {
                        string cpdName = dtRow.Items[pageDim.Index].Name;
                        if (!pageDim.SelectedNames.Contains(cpdName))
                        {
                            isVisible = false;
                            break;
                        }
                    }
                }
                #endregion
                */
                if (isVisible)
                {
                    visualXRows.Add(rowIndex);

                    // 实际的列维渲染行索引
                    int dActualIndex = 0;
                    for (int dIndex = 0; dIndex < xdCount; dIndex++)
                    {
                        string codePath = dtRow.GetCodePath(dIndex);
                        XDItem dItem = dtRow.Items[dIndex];
                        /*
                        MergeInfo mergeInfo = null;*/
                        bool isRepeat = false;
                        /*
                        if (_xMergeDict.ContainsKey(dIndex))
                        {
                            MergeInfo existMerge = _xMergeDict[dIndex];
                            if (existMerge.CodePath == codePath)
                            {
                                existMerge.RepeatCount++;
                                isRepeat = true;
                            }
                            else
                            {
                                mergeInfo = existMerge;

                                MergeInfo newMergeInfo = new MergeInfo();
                                newMergeInfo.BeginCell = _worksheet.XGetCell(beginRow + dActualIndex, firstCol + aColIndex);
                                newMergeInfo.RepeatCount = 0;
                                newMergeInfo.CodePath = codePath;
                                _xMergeDict[dIndex] = newMergeInfo;
                            }
                        }
                        else
                        {
                            mergeInfo = new MergeInfo();
                            mergeInfo.BeginCell = _worksheet.XGetCell(beginRow + dActualIndex, firstCol + aColIndex);
                            mergeInfo.RepeatCount = 0;
                            mergeInfo.CodePath = codePath;
                            _xMergeDict.Add(dIndex, mergeInfo);
                        }

                        if (mergeInfo != null && mergeInfo.RepeatCount > 0)
                        {
                            Excel.Range mergeRange = _worksheet.XGetRange(mergeInfo.BeginCell.Row, mergeInfo.BeginCell.Column,
                                mergeInfo.BeginCell.Row,
                                mergeInfo.BeginCell.Column + mergeInfo.RepeatCount);
                            _worksheet.XGetRange(mergeInfo.BeginCell.Row, mergeInfo.BeginCell.Column,
                                mergeInfo.BeginCell.Row,
                                mergeInfo.BeginCell.Column + mergeInfo.RepeatCount).Merge(mergeInfo.BeginCell.MergeCells);


                            mergeRange.Interior.Color = 0xffcf9c;
                            mergeRange.Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight = Excel.XlBorderWeight.xlThin;
                            mergeRange.Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = Excel.XlBorderWeight.xlThin;
                            mergeRange.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlThin;
                            mergeRange.Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThin;
                            mergeRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
                        }*/

                        if (!isRepeat)
                        {
                            // 重复出现的维度名称，不在创建到excel上
                            Excel.Range range = worksheet.XGetCell(beginRow + dActualIndex, firstCol + aColIndex);
                            range.Value2 = dItem.Name;
                            range.Interior.Color = 0xffcf9c;
                            range.Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight = Excel.XlBorderWeight.xlThin;
                            range.Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = Excel.XlBorderWeight.xlThin;
                            range.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlThin;
                            range.Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThin;
                            range.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
                        }
                        dActualIndex++;
                    }
                    aColIndex++;
                }
            }
            /*
            #region 检查最后的单元格
            foreach (var xmergeKey in _xMergeDict.Keys)
            {
                MergeInfo mergeInfo = _xMergeDict[xmergeKey];
                if (mergeInfo.RepeatCount > 0)
                {
                    Excel.Range mergeRange = _worksheet.XGetRange(mergeInfo.BeginCell.Row, mergeInfo.BeginCell.Column,
                        mergeInfo.BeginCell.Row, mergeInfo.BeginCell.Column + mergeInfo.RepeatCount);
                    _worksheet.XGetRange(mergeInfo.BeginCell.Row, mergeInfo.BeginCell.Column,
                        mergeInfo.BeginCell.Row, mergeInfo.BeginCell.Column + mergeInfo.RepeatCount).Merge(mergeInfo.BeginCell.MergeCells);

                    mergeRange.Interior.Color = 0xffcf9c;
                    mergeRange.Borders[Excel.XlBordersIndex.xlEdgeLeft].Weight = Excel.XlBorderWeight.xlThin;
                    mergeRange.Borders[Excel.XlBordersIndex.xlEdgeTop].Weight = Excel.XlBorderWeight.xlThin;
                    mergeRange.Borders[Excel.XlBordersIndex.xlEdgeRight].Weight = Excel.XlBorderWeight.xlThin;
                    mergeRange.Borders[Excel.XlBordersIndex.xlEdgeBottom].Weight = Excel.XlBorderWeight.xlThin;
                    mergeRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
                }
            }
            #endregion
            #endregion

            #region 更新内容区域
            docMatrix.VisualRect.BeginRow = beginRow;
            docMatrix.VisualRect.EndRow = (beginRow + visualYRows.Count + xdCount);
            docMatrix.VisualRect.BeginCol = beginCol;
            docMatrix.VisualRect.EndCol = (beginCol + visualXRows.Count + ydCount);
            Debug.WriteLine("set rect:{0},{1}-{2},{3}",
                docMatrix.VisualRect.BeginRow,
                docMatrix.VisualRect.BeginCol,
                docMatrix.VisualRect.EndRow,
                docMatrix.VisualRect.EndCol);
            #endregion

            docMatrix.VisualRect.YRowIndexList = visualYRows;
            docMatrix.VisualRect.XRowIndexList = visualXRows;

            docMatrix.VisualRect.DataBeginRow = firstRow;
            docMatrix.VisualRect.DataBeginCol = firstCol;
            docMatrix.VisualRect.DataEndRow = docMatrix.VisualRect.DataBeginRow + docMatrix.VisualRect.YRowIndexList.Count;
            docMatrix.VisualRect.DataEndCol = docMatrix.VisualRect.DataBeginCol + docMatrix.VisualRect.XRowIndexList.Count;*/
        }
    }
}
